local haoyin = fk.CreateSkill{
    name = "ym2__haoyin",
    tags = {Skill.Compulsory},
}
Fk:loadTranslationTable{
    ["ym2__haoyin"] = "皓寅",
    [":ym2__haoyin"] = "锁定技，你每回合使用的首张【杀】结算三次；且单次伤害基数任意，但总计不超过此【杀】原伤害基数。",
    ["#ym2__haoyin-prompt"] = "皓寅：分配每一张【杀】的伤害基数",
    ["#ym2__haoyin1"] = "第一张",
    ["#ym2__haoyin2"] = "第二张",
    ["#ym2__haoyin3"] = "第三张",

    ["$ym2__haoyin1"] = "父祖具戎马宇内之风，子莫敢不效。",
    ["$ym2__haoyin2"] = "青霜染虎鬓，踏雪征千里。",
}
haoyin:addEffect(fk.CardUsing,{
    anim_type = "offensive",
    can_trigger = function (self, event, target, player, data)
        if player:hasSkill(haoyin.name) and data.card.trueName == "slash" then
            local use_events = player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
                return e.data.from == player and e.data.card.trueName == "slash"
            end, Player.HistoryTurn)
            return #use_events == 1 and use_events[1].data == data
        end
    end,
    on_cost = Util.TrueFunc,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local num = 1
        num = (data.additionalDamage or 0) + num
        local cards = table.random(room:getBanner(haoyin.name),num)
        local result = room:askToCustomDialog(player, {
            skill_name = haoyin.name,
            qml_path = "packages/ym_Al/qml/HaoyinBox.qml",
            extra_data = {
                "#ym2__haoyin-prompt",
                cards,
                {}, "#14708A", "#ym2__haoyin1",
                {}, "#14708A", "#ym2__haoyin2",
                {}, "#14708A", "#ym2__haoyin3",
            }
        })
        local a = math.random(0,2)
        local b = math.random(0,2-a)
        local c = 2 - a - b
        local damage = {a,b,c}
        if result ~= "" then
            local d = json.decode(result)
            damage = {#d[1],#d[2],#d[3]}
        end
        data.additionalDamage = 0
        data.extra_data = data.extra_data or {}
        data.extra_data.haoyin_damage = damage[1]
        data.extra_data.haoyin = {
            card = data.card,
            from = player,
            tos = data.extra_data.haoyin_orig_info.tos,
            subTos = data.extra_data.haoyin_orig_info.subTos,
            damage = damage
        }
    end,
})
haoyin:addEffect(fk.PreCardUse, {
    can_refresh = function(self, event, target, player, data)
        return target == player and player:hasSkill(haoyin.name) and
        not table.contains(data.card.skillNames, haoyin.name)
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        if #data.tos > 0 then
            --使用前就确定目标，不管后续增减
            data.extra_data = data.extra_data or {}
            data.extra_data.haoyin_orig_info = {
                tos = data.tos,
                subTos = data.subTos or {},
            }
        end
    end,
})
local parsehaoyinUseStruct = function (player, data)
    local card = Fk:cloneCard(data.card.name,data.card.suit,data.card.number)
    card.skillName = haoyin.name
    if player:prohibitUse(card) then return end
    local all_tos = {}
    for _, target in ipairs(data.tos) do
        local selected = {}
        --有不合法目标或副目标则立即结束
        if target.dead or player:isProhibited(target, card) then return end
        if #data.subTos > 0 then
            local selected_targets = { target }
            for _, subTo in ipairs(data.subTos[table.indexOf(data.tos, target)]) do
                if subTo.dead then return end
                if not card.skill:modTargetFilter(player, subTo, selected_targets, card) then return nil end
                table.insert(selected_targets, subTo)
            end
        else
            if not card.skill:modTargetFilter(player, target, selected, card) then return end
        end
        table.insert(selected, target)
        table.insert(all_tos, target)
    end
    --aoe目标改为默认目标
    if card.multiple_targets and card.skill:getMinTargetNum(player) == 0 then
        all_tos = card:getDefaultTarget(player, { bypass_distances = true, bypass_times = true })
    end
    return {
        from = player,
        tos = all_tos,
        subTos = data.subTos,
        card = card,
        extraUse = true,
    }
end
haoyin:addEffect(fk.CardUseFinished, {
    mute = true,
    is_delay_effect = true,
    can_trigger = function(self, event, target, player, data)
        if data.extra_data and data.extra_data.haoyin and not player.dead then
            local dat = table.simpleClone(data.extra_data.haoyin)
            if dat.from == player then
                local use = parsehaoyinUseStruct(player, dat)
                if use then
                    event:setCostData(self, {extra_data = use})
                    return true
                end
            end
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local dat = table.simpleClone(data.extra_data.haoyin)
        local use = table.simpleClone(event:getCostData(self).extra_data)
        for i = 1, 2, 1 do
            use.extra_data = use.extra_data or {}
            use.extra_data.haoyin_damage = dat.damage[i+1]
            room:useCard(use)
            if player.dead then break end
            use = parsehaoyinUseStruct(player, dat)
            if use == nil then break end
        end
    end,
})
haoyin:addEffect(fk.DamageCaused,{
    can_refresh = function (self, event, target, player, data)
        if target == player then
            local e = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
            if e then
                local use = e.data
                return use.extra_data and use.extra_data.haoyin_damage
            end
        end
    end,
    on_refresh = function (self, event, target, player, data)
        local e = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
        if e then
            local use = e.data
            if use.extra_data and use.extra_data.haoyin_damage then
                local num = use.extra_data.haoyin_damage - 1
                if (data.damage + num) == 0 then
                    data.damage = 0
                    --data.isVirtualDMG = true
                else
                    data.damage = data.damage + num
                end
            end
        end
    end,
})
haoyin:addAcquireEffect(function (self, player, is_start)
    local room = player.room
    if room:getBanner(haoyin.name) == nil then
        local cards = {}
        for i = 1, 5, 1 do
            local id = room:printCard("slash").id
            table.insertIfNeed(cards,id)
        end
        room:setBanner(haoyin.name, cards)
    end
end)
return haoyin